/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



import com.controllers.CompanyDenominationController;
import com.controllers.CreditController;
import com.controllers.CrudUtil;
import com.entities.CompanyDenomination;
import com.entities.Credit;
import com.entities.Transactions;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author ben
 */
@Named(value = "experiments")
@RequestScoped
public class Experiments implements Serializable {

    @EJB
    private CompanyDenominationController companyDenominationController;
    @EJB
    private CreditController creditController;
    @EJB
    private CrudUtil crudUtil;
    /**
     * Creates a new instance of Experiments
     */
    private List<Transactions> transactions;
    private Transactions etransactions;
    private Long selectedCompanyDenomination;
    private double total;

    
    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public Long getSelectedCompanyDenomination() {
        return selectedCompanyDenomination;
    }

    public void setSelectedCompanyDenomination(Long selectedCompanyDenomination) {
        this.selectedCompanyDenomination = selectedCompanyDenomination;
    }

    public List<Transactions> getTransactions() {
        CompanyDenomination c = companyDenominationController.findById(this.selectedCompanyDenomination);
        // System.out.println(c.getTransactions());

        List<Transactions> t = c.getTransactions();
        double total = 0;

        for (Iterator<Transactions> it = t.iterator(); it.hasNext();) {

            Transactions transactions1 = it.next();
            total = total + c.getDenomination().getAmount();

            System.out.println(transactions1.getId());

        }
        this.total = total;
        System.out.println(total);

        return t;

    }

    public void setTransactions(List<Transactions> transactions) {
        this.transactions = transactions;
    }
    private Credit credit;

    public Credit getCredit() {
        return credit;
    }

    public void setCredit(Credit credit) {
        this.credit = credit;
    }

    public Experiments() {
        credit = new Credit();

    }

    public String testGetCreditByCompanyDenomination() {
        CompanyDenomination companyDenomination = companyDenominationController.findById(11L);
        credit = creditController.getCreditByCompanyDenomination(companyDenomination);
        return null;
    }

    public String testSetToUse() {
        CompanyDenomination companyDenomination = companyDenominationController.findById(11L);
        credit = creditController.getCreditByCompanyDenomination(companyDenomination);

        credit.setDateUsed(new Date());
        credit.setUsed(Boolean.TRUE);
        //  crudUtil.update(credit);

        Transactions transactions = new Transactions();
        transactions.setMpowerAccountNumber("488575");
        //transactions.setCredit(credit);
        //  crudUtil.persist(transactions);
        //System.out.println(transactions.getId());

        //credit.setTransactions(transactions);
        //transactions.setCompanyDenomination(companyDenomination);
        transactions.setCredit(credit);
        transactions.setCompanyDenomination(companyDenomination);
        crudUtil.update(credit);
        crudUtil.update(transactions);

        // credit.setTransactions(transactions);
        //  System.out.println(companyDenomination.getTransactions().size());

        return null;

    }

    public String testRetrieval() {
        return "viewSalesByVoucher";
    }
    String host = "smtp.gmail.com";
    String port = "587";
    String username = "purcase.qrcodes@gmail.com";
    String password = "!purchase.qrcodes!";

    public String sendMail() {
        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        String msgBody = "Hello from the time bomb!";
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("purcase.qrcodes@gmail.com", "purchase.qrcodes"));
            msg.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("brainychip@gmail.com", "brainychip@gmail.com"));
            msg.setSubject("Receipt of QR-Code");
            msg.setText(msgBody);

            CompanyDenomination companyDenomination = companyDenominationController.findById(11L);
            credit = creditController.getCreditByCompanyDenomination(companyDenomination);

            Multipart mp = new MimeMultipart();



            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(msgBody, "text/html");
            mp.addBodyPart(htmlPart);
            MimeBodyPart attachment = new MimeBodyPart();
            attachment.setFileName("qrcode.jpeg");
            attachment.setContent(credit.getImage(), "image/jpeg");
            mp.addBodyPart(attachment);
            msg.setContent(mp);
            Transport.send(msg);

            System.out.println("Mail Successfully sent");
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Experiments.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }

        return null;
    }

    public CompanyDenominationController getCompanyDenominationController() {
        return companyDenominationController;
    }

    public void setCompanyDenominationController(CompanyDenominationController companyDenominationController) {
        this.companyDenominationController = companyDenominationController;
    }

    public CreditController getCreditController() {
        return creditController;
    }

    public void setCreditController(CreditController creditController) {
        this.creditController = creditController;
    }

    public CrudUtil getCrudUtil() {
        return crudUtil;
    }

    public void setCrudUtil(CrudUtil crudUtil) {
        this.crudUtil = crudUtil;
    }

    public Transactions getEtransactions() {
        return etransactions;
    }

    public void setEtransactions(Transactions etransactions) {
        this.etransactions = etransactions;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
